<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据管理 - 外卖平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f5f5;
            color: #333;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        h1 {
            color: #ff6b6b;
            margin-bottom: 30px;
            text-align: center;
        }
        
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #eee;
            border-radius: 8px;
        }
        
        .section h2 {
            color: #333;
            margin-bottom: 15px;
        }
        
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            margin: 5px;
            font-size: 14px;
        }
        
        .btn-primary {
            background: #ff6b6b;
            color: white;
        }
        
        .btn-primary:hover {
            background: #ee5a24;
            transform: translateY(-2px);
        }
        
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        
        .btn-danger:hover {
            background: #c82333;
        }
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background: #218838;
        }
        
        .btn-info {
            background: #17a2b8;
            color: white;
        }
        
        .btn-info:hover {
            background: #138496;
        }
        
        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        
        .stat-card {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
        }
        
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #ff6b6b;
        }
        
        .stat-label {
            color: #666;
            font-size: 14px;
        }
        
        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
            text-align: center;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .warning {
            background: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🗄️ 数据管理中心</h1>
        
        <div id="message"></div>
        
        <!-- 数据统计 -->
        <div class="section">
            <h2>📊 当前数据统计</h2>
            <div class="stats" id="stats">
                <!-- 统计数据将通过JavaScript动态加载 -->
            </div>
        </div>
        
        <!-- 数据管理 -->
        <div class="section">
            <h2>🔧 数据管理操作</h2>
            <div class="warning">
                ⚠️ 注意：重新初始化数据将清空所有现有数据，包括用户、商家、菜品和订单信息！
            </div>
            <button class="btn btn-primary" onclick="reinitData()">🔄 重新初始化数据（10个商家+100个菜品）</button>
            <button class="btn btn-danger" onclick="clearAllData()">🗑️ 清空所有数据</button>
            <button class="btn btn-info" onclick="refreshStats()">🔄 刷新统计</button>
        </div>
        
        <!-- 数据导出/导入 -->
        <div class="section">
            <h2>📤 数据导出/导入</h2>
            <button class="btn btn-success" onclick="exportData()">📤 导出数据</button>
            <button class="btn btn-info" onclick="importData()">📥 导入数据</button>
            <input type="file" id="fileInput" accept=".json" style="display: none;" onchange="handleFileImport(event)">
        </div>
        
        <!-- 快速操作 -->
        <div class="section">
            <h2>⚡ 快速操作</h2>
            <button class="btn btn-primary" onclick="goToHome()">🏠 返回首页</button>
            <button class="btn btn-info" onclick="goToUser()">👤 进入用户端</button>
            <button class="btn btn-info" onclick="goToMerchant()">🏪 进入商家端</button>
        </div>
    </div>

    <script src="database.js"></script>
    <script>
        // 页面加载时显示统计信息
        window.onload = function() {
            refreshStats();
        };
        
        // 刷新统计数据
        function refreshStats() {
            const users = db.getUsers();
            const merchants = db.getMerchants();
            const dishes = db.getDishes();
            const orders = db.getOrders();
            
            document.getElementById('stats').innerHTML = `
                <div class="stat-card">
                    <div class="stat-number">${users.length}</div>
                    <div class="stat-label">用户数量</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number">${merchants.length}</div>
                    <div class="stat-label">商家数量</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number">${dishes.length}</div>
                    <div class="stat-label">菜品数量</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number">${orders.length}</div>
                    <div class="stat-label">订单数量</div>
                </div>
            `;
        }
        
        // 重新初始化数据
        function reinitData() {
            if (confirm('确定要重新初始化数据吗？这将清空所有现有数据并加载10个商家和100个菜品！')) {
                try {
                    db.forceReinit();
                    showMessage('数据重新初始化成功！现在有10个商家和100个菜品。', 'success');
                    refreshStats();
                } catch (error) {
                    showMessage('数据初始化失败：' + error.message, 'error');
                }
            }
        }
        
        // 清空所有数据
        function clearAllData() {
            if (confirm('确定要清空所有数据吗？此操作不可恢复！')) {
                try {
                    db.clearAllData();
                    showMessage('所有数据已清空！', 'success');
                    refreshStats();
                } catch (error) {
                    showMessage('清空数据失败：' + error.message, 'error');
                }
            }
        }
        
        // 导出数据
        function exportData() {
            try {
                const data = db.exportData();
                const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `外卖平台数据_${new Date().toISOString().split('T')[0]}.json`;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
                showMessage('数据导出成功！', 'success');
            } catch (error) {
                showMessage('数据导出失败：' + error.message, 'error');
            }
        }
        
        // 导入数据
        function importData() {
            document.getElementById('fileInput').click();
        }
        
        // 处理文件导入
        function handleFileImport(event) {
            const file = event.target.files[0];
            if (!file) return;
            
            const reader = new FileReader();
            reader.onload = function(e) {
                try {
                    const data = JSON.parse(e.target.result);
                    db.importData(data);
                    showMessage('数据导入成功！', 'success');
                    refreshStats();
                } catch (error) {
                    showMessage('数据导入失败：' + error.message, 'error');
                }
            };
            reader.readAsText(file);
        }
        
        // 显示消息
        function showMessage(text, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="message ${type}">${text}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 5000);
        }
        
        // 导航函数
        function goToHome() {
            window.location.href = 'index.html';
        }
        
        function goToUser() {
            window.location.href = 'user.html';
        }
        
        function goToMerchant() {
            window.location.href = 'merchant.html';
        }
    </script>
</body>
</html>
